<template>
  <div class="user-log">
    <el-card>
      <!-- 搜索 -->
      <div class="search">
        <el-form :inline="true">
          <el-form-item label="用户ID：">
            <el-input
              v-model="params.userId"
              clearable
              placeholder="请输入用户ID"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="用户名：">
            <el-input
              v-model="params.userName"
              clearable
              placeholder="请输入用户名"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="handleQuery">搜索</el-button>
            <el-button :disabled="loading" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 日志列表 -->
      <el-table :data="tableData" style="width: 100%" :loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="userName" label="用户名" width="120" />
        <el-table-column prop="realName" label="真实姓名" width="120" />
        <el-table-column prop="content" label="动态" min-width="300" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
      </el-table>

      <div style="margin-top: 20px">
        <el-pagination
          v-model:current-page="params.pageIndex"
          v-model:page-size="params.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from "vue";
import { useRoute } from "vue-router";
import UserAPI from "@/api/system/user.api";
import { ElMessage } from "element-plus";

interface LogType {
  id: number;
  userName: string;
  realName: string;
  content: string;
  createTime: string;
}

const route = useRoute();

const tableData = ref<LogType[]>([]);
const total = ref(0);
const loading = ref(false);

// 查询参数
const params = reactive({
  userId: null as number | null,
  userName: null as string | null,
  pageIndex: 1,
  pageSize: 10,
});

// 获取日志列表
function getLogList() {
  loading.value = true;
  UserAPI.getUserEventPage(params)
    .then((data: any) => {
      tableData.value = data.list || [];
      total.value = data.total || 0;
    })
    .catch(() => {
      ElMessage.error("获取日志列表失败");
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleQuery() {
  params.pageIndex = 1;
  getLogList();
}

function handleReset() {
  params.userId = null;
  params.userName = null;
  params.pageIndex = 1;
  getLogList();
}

function handleSizeChange(size: number) {
  params.pageSize = size;
  params.pageIndex = 1;
  getLogList();
}

function handleCurrentChange(page: number) {
  params.pageIndex = page;
  getLogList();
}

onMounted(() => {
  // 从路由参数中获取搜索条件
  const userId = route.query.userId ? Number(route.query.userId) : null;
  const userName = (route.query.userName as string) || null;

  if (userId) {
    params.userId = userId;
  }
  if (userName) {
    params.userName = userName;
  }

  getLogList();
});
</script>

<style scoped lang="scss">
.user-log {
  padding: 16px;
}
</style>
